//
//  FSLoginViewController.m
//  WZSOA
//
//  Created by 刘沿 on 16/5/31.
//  Copyright © 2016年 刘沿. All rights reserved.
//

#import "FSLoginViewController.h"
#import "XmlParseHelper.h"
#import "FSCSViewController.h"

@interface FSLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *usernameTextfield;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

//记住账号属性
@property (weak, nonatomic) IBOutlet UIButton *remerberAccount;


//自动登录属性
@property (weak, nonatomic) IBOutlet UIButton *remerberPassword;


@end

@implementation FSLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] length] != 0) {
        
        self.usernameTextfield.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    }
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"pwdId"] length] != 0) {
        
        self.passwordTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"pwdId"];
    }
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"autoLogin"] isEqualToString:@"Y"]) {
        self.remerberPassword.selected = YES;
    }

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    // 注销第一响应者
    [textField resignFirstResponder];
    
    return YES;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.usernameTextfield resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    
}

//登录按钮
- (IBAction)loginBtnClick:(id)sender {
    
    [self.usernameTextfield resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    
    if (self.usernameTextfield.text.length == 0) {
        
        [self.view makeToast:@"请输入账号"];
        return;
    }
    
    if (self.passwordTextField.text.length == 0) {
        
        [self.view makeToast:@"请输入密码"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.usernameTextfield.text forKey:@"userId"];
    [params setObject:self.passwordTextField.text forKey:@"pwdId"];
    
    [HttpTools postWithURL:KexchURL arrays:@[@{@"userId":self.usernameTextfield.text},@{@"pwdId":self.passwordTextField.text}] method:@"validationUser" success:^(id JSON) {
        //NSLog(@"%@",JSON);
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        XmlParseHelper *parse = [[XmlParseHelper alloc] initWithData:JSON];
        
        NSArray *resultArray = [parse selectNodes:@"errmsg"];
        
        NSString *resultTag = nil;
        if (resultArray.count) {
            NSDictionary *dict = [resultArray lastObject];
            
            resultTag = dict[@"text"];
        }
        
        FSCSViewController *ch = [self.storyboard instantiateViewControllerWithIdentifier:@"CSVC"];

        switch ([resultTag integerValue]) {
            case 1:
                [self.view makeToast:@"您的用户名不存在，请确认后再输"];
                break;
            case 2:
                self.view.window.rootViewController = ch;
                // 存储账号密码
                if (self.remerberAccount.selected) {
                    [[NSUserDefaults standardUserDefaults] setObject:self.usernameTextfield.text forKey:@"userId"];
                    [[NSUserDefaults standardUserDefaults] setObject:self.passwordTextField.text forKey:@"pwdId"];
                    
                }else{
                    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"userId"];
                    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"pwdId"];
                }
                
                if (!self.remerberPassword.selected) {
                    [[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"autoLogin"];
                }else{
                    [[NSUserDefaults standardUserDefaults] setObject:@"Y" forKey:@"autoLogin"];
                }
                break;
            case 3:
                [self.view makeToast:@"密码错误"];
                
                break;
                
            default:
                break;
        }
        
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
    }];
    
}

//记住账号
- (IBAction)remerberAccount:(id)sender {
    
    self.remerberAccount.selected = !self.remerberAccount.selected;
    
    //
    if (!self.remerberAccount.selected) {
        self.remerberPassword.selected = self.remerberAccount.selected;
    }
    
}

//记住密码
- (IBAction)remerberPassword:(id)sender {
    
    self.remerberPassword.selected = !self.remerberPassword.selected;
}


@end
